package com.anysoft.register;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.anysoft.MentorConst;
import com.anysoft.utils.MentorLogger;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.anysoft.HostConstant;
import com.anysoft.R;
import com.anysoft.adapter.TaxPayerAdapter;
import com.anysoft.base.BaseActivity;
import com.anysoft.bean.BaseBean;
import com.anysoft.bean.QueryCertificateTypeBean;
import com.anysoft.bean.QueryCountryBean;
import com.anysoft.bean.QueryProfessionBean;
import com.anysoft.bean.QueryProvinceCityBean;
import com.anysoft.bean.QueryTaxProvinceCityBean;
import com.anysoft.bean.SexBean;
import com.anysoft.camera.CaptureActivity;
import com.anysoft.event.RegisterEvent;
import com.anysoft.event.RegisterSuccessEvent;
import com.anysoft.bean.TaxPayerInfo;
import com.anysoft.linstener.OnNextLinstener;
import com.anysoft.login.LoginActivity;
import com.anysoft.me.utils.PermissionManager;
import com.anysoft.utils.AESUtils;
import com.anysoft.utils.AnalyticsEventUtils;
import com.anysoft.utils.CommonUtil;
import com.anysoft.utils.DisplayUtil;
import com.anysoft.utils.HttpUtils;
import com.anysoft.utils.ParamUtil;
import com.anysoft.utils.StringUtils;
import com.anysoft.utils.TipPopwindowHelper;
import com.anysoft.view.CustomDividerItemDecoration;
import com.anysoft.view.HWEditText;
import com.anysoft.view.picker.DateEndPickerView;
import com.anysoft.view.picker.DatePickerView;
import com.anysoft.view.picker.NomarlPickerPopupWindow;
import com.google.gson.Gson;
import com.kyleduo.switchbutton.SwitchButton;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import butterknife.OnClick;

import static com.anysoft.MentorApplication.context;
import static com.anysoft.MentorConst.CUSTOMER_SERVICE_TELEPHONE_NUMBERS;
import static com.anysoft.utils.ParamUtil.isSelectTaxCountry;

/**
 * Created by lty.
 */

public class RegisterStep2Activity extends BaseActivity implements View.OnClickListener, PermissionManager.OnPermissionListener {

    private final static int CAPTURE = 104;//扫码
    LinearLayout rootview;
    Button leftButton;
    TextView title;
    Button rightButton;
    ImageView iv_register_step_2;
    TextView tv_register_step_2;
    RecyclerView mRecyclerView;

    //长期有效
    CheckBox cb_register_step_2_id_time;
    //证件有效期
    TextView tv_register_step_2_id_time;
    //性别
    TextView tv_register_step_2_sex;
    //职业
    ImageView iv_register_step_2_occupation;
    TextView tv_register_step_2_occupation;
    RelativeLayout rl_register_step_2_occupation;
    //职务
    HWEditText et_register_step_2_duty;
    //出生日期
    TextView tv_register_step_2_birthday;
    /*邮箱*/
    HWEditText et_register_step_2_emil;
    /*居住地  省/市*/
    TextView tv_register_step_2_province;
    RelativeLayout rl_register_step_2_province;
    /*居住地  市/区*/
    TextView tv_register_step_2_city;
    RelativeLayout rl_register_step_2_city;
    /*邮编*/
    HWEditText et_register_step_2_postcode;
    /*详细地址*/
    HWEditText et_register_step_2_address;

    /*推荐人手机号*/
    HWEditText et_register_step_2_id_tjr;
    /*二维码*/
    ImageView iv_register_step_2_tjr;

    //3.07新增 账户是否由他人控制
    SwitchButton sbtn_is_others_control; //是否由他人控制
    LinearLayout ll_others_control_container;
    HWEditText et_others_control_name;
    HWEditText et_control_identity;
    TextView tv_control_identityType;
    ImageView iv_control_identityType;
    TextView tv_control_identity_validity;
    CheckBox cb_control_identity_validity_time;
    HWEditText et_control_cause;
    TextView tv_control_length;

    //3.07新增 实际受益人是否是本人
    SwitchButton sbtn_is_beneficial_owner; //是否本人收益
    LinearLayout ll_beneficial_owner_container;
    HWEditText et_beneficial_owner_name;
    HWEditText et_beneficial_owner_identity;
    TextView tv_beneficial_identityType;
    ImageView iv_beneficial_identityType;
    TextView tv_beneficial_identity_validity;
    CheckBox cb_beneficial_identity_validity_time;
    HWEditText et_beneficial_owner_cause;//受益人说明原因
    TextView tv_beneficial_owner_length;

    //3.07新增 不良记录
    SwitchButton sbtn_is_bad_record; //是否有不良记录
    LinearLayout ll_bad_record_container;
    HWEditText et_bad_record;
    TextView tv_bad_record_length;

    //个人涉税
    LinearLayout ll_tax_layout; //涉税根布局
    //请选择居民身份 声明
    TextView tv_register_step_2_identity_statement;
    ImageView iv_register_step_2_identity_statement;
    //3.0.8新增 红色问号提示
    RelativeLayout rl_tips_flag;
    TextView tv_tips_flag;

    /*3.0.9 改动 现税收居民信息*/
    LinearLayout ll_tax_user_info_layout;
    HWEditText et_tax_surname; //姓
    HWEditText et_tax_name; //名

    /*3.0.9 改动 现居住详细地址*/
    LinearLayout ll_tax_live_address_layout;
    //请选择国家
    RelativeLayout rl_tv_tax_live_address_country_layout;
    TextView tv_tax_live_address_country;
    RelativeLayout rl_tax_live_address_province_layout;
    //请选择省
    TextView tv_tax_live_address_province;
    HWEditText hwet_tax_live_address_province;
    ImageView iv_tax_live_address_province;
    //请选择市
    RelativeLayout rl_tax_live_address_city_layout;
    TextView tv_tax_live_address_city;
    HWEditText hwet_tax_live_address_city;
    ImageView iv_tax_live_address_city;
    /*中文地址*/
    LinearLayout ll_live_chinese_address_container;
    //输入中文地址
    HWEditText et_tax_live_chinese_address;
    //输入拼音地址
    HWEditText et_tax_live_pinyin_address;
    /*英文地址*/
    LinearLayout ll_live_nochinese_address_container;
    //输入英文地址
    HWEditText et_tax_live_english_address;


    /*3.0.9改动 出生地详细地址*/
    LinearLayout ll_tax_birthplace_address_layout;
    //请选择国别
    RelativeLayout rl_tax_birthplace_address_country;
    TextView tv_tax_birthplace_address_country;
    //请选择省
    RelativeLayout rl_tax_birthplace_address_province_layout;
    TextView tv_tax_birthplace_address_province;
    //请输入省
    HWEditText hwet_tax_birthplace_address_province;
    ImageView iv_tax_birthplace_address_province;
    //请选择市
    RelativeLayout rl_tax_birthplace_address_city_layout;
    TextView tv_tax_birthplace_address_city;
    HWEditText hwet_tax_birthplace_address_city;
    ImageView iv_tax_birthplace_address_city;

    //其他纳税人
    TextView tv_other_taxpayer;
    //声明复选框
    CheckBox cb_ident_shengming_info;

    private NomarlPickerPopupWindow nomarlPickerPopupWindow;
    private int currentPosition;
    private int selectPosition = -1;
    private ArrayList<String> list = new ArrayList<>();
    private List<QueryProfessionBean.DataBean> professionList = new ArrayList<>(); //职业数据集合
    private List<SexBean> sexList = new ArrayList<>();
    private boolean idPopShow = false;
    /**
     * 1.性别，2.职业 3.居住地址省/市 4. 居住地址市/区 5:居民身份类型 6：现居地国别 7：出生地国别  8，9，10：为 识别号的 国别1，2，3
     * 11：现居地 省 12 现居地 市
     * 13：出生地 省  14 出生地 市
     * 15: 账户他人控制点击身份证
     * 16: 受益人点击身份证
     */
    private int currentType = 1;

    private DatePickerView datePickerView;

    private DateEndPickerView dateEndPickerView;

    private List<QueryTaxProvinceCityBean.DataBean> taxProvinceList = new ArrayList<>();
    private List<QueryTaxProvinceCityBean.DataBean> taxCityList = new ArrayList<>();
    private List<QueryProvinceCityBean.DataBean> provinceList = new ArrayList<>();
    private List<QueryProvinceCityBean.DataBean> cityList = new ArrayList<>();
    private String selectedProvince = "";
    private RegisterEvent registerEvent;
    private String registEmil, registPostCode, registAddress, id_address;

    private int datePickerType; //证件有效期选择框类型 100: 开户者证件有效期 101: 账户控制他人证件有效期 102: 受益人他人证件有效期

    private DatePickerView.OnTimeSelectListener mDatePickerSelectLinstener = new DatePickerView
            .OnTimeSelectListener() {
        @Override
        public void onTimeSelect(Date date) {
            switch (datePickerType) {
                case 100:
                    tv_register_step_2_id_time.setText(getTime(date));
                    cb_register_step_2_id_time.setChecked(false);
                    break;
                case 101:
                    tv_control_identity_validity.setText(getTime(date));
                    cb_control_identity_validity_time.setChecked(false);
                    break;
                case 102:
                    tv_beneficial_identity_validity.setText(getTime(date));
                    cb_beneficial_identity_validity_time.setChecked(false);
                    break;
            }

        }
    };
    //3.0.7新增
    private List<QueryCertificateTypeBean.DataBean> identityTypeList = new ArrayList<>(); //他人控制及实际受益人证件类型
    private boolean isFirstComing = true;
    private String registOccupation; //职务
    private String controllerName;  //控制人姓名
    private String controllerCertType = "0"; //控制人证件类型
    private String controllerCertNo; //控制人证件号
    private String controllerCertValidTerm; //控制人证件有效期
    private String controllerInstruction; //控制人说明

    private String beneficiaryName;  //实际受益人姓名
    private String beneficiaryCertType = "0";  //实际受益证件类型
    private String beneficiaryCertNo;  //实际受益人证件号
    private String beneficiaryCertValidTerm;  //实际受益人证件有效期
    private String beneficiaryInstruction;  //实际受益人证件有效期

    private String badCreditRecords;  //有无不良记录
    private int maxTaxpayerSize = 10;
    private TextView tv_tax_ident_num_country;
    private List<TaxPayerInfo> taxPayersContainer = new ArrayList<>(); //展示的集合容器
    private List<TaxPayerInfo> taxPayersTotal = new ArrayList<>(); //集合总大小
    private TaxPayerAdapter taxPayerAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_register_step_2);
        initContentView();
        title.setText("开户");

        registerEvent = (RegisterEvent) getIntent().getSerializableExtra("registerEvent");

        getProfession(false);
        getIndentityType(false);
        //3.0.9新增
        initRecyclerViewLayout();
        taxLayoutInit();
        getCountry(false);
        getTaxProvince(false);
        if ("0".equals(registerEvent.getIdentityType())) {
            String birthday = registerEvent.getIdentityNo().substring(6, 10) + "-"
                    + registerEvent.getIdentityNo().substring(10, 12) + "-"
                    + registerEvent.getIdentityNo().substring(12, 14);
            tv_register_step_2_birthday.setText(birthday);
            tv_register_step_2_birthday.setClickable(false);
            int sexNum = Integer.parseInt(registerEvent.getIdentityNo().substring(16, 17));
            if (sexNum % 2 == 0) {
                tv_register_step_2_sex.setText("女");
                sex = "0";
            } else {
                tv_register_step_2_sex.setText("男");
                sex = "1";
            }
            if ("1".equals(registerEvent.getCertificateValidEver())) {
                cb_register_step_2_id_time.setChecked(true);
            } else {
                cb_register_step_2_id_time.setChecked(false);
            }
            tv_register_step_2_id_time.setText(registerEvent.getIdValidDate());
            et_register_step_2_address.setText(registerEvent.getAddress());

        } else {
            //弹出时间选择器
            tv_register_step_2_birthday.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    if (!isFastDoubleClick()) {
                        collapseSoftInputMethod(view.getWindowToken());
                        //时间选择器
                        dateEndPickerView = new DateEndPickerView(mActivity, MentorConst.DateType.YEAR_MONTH_DAY);
                        //控制时间范围
                        Calendar calendarEnd = Calendar.getInstance();
                        //要在setTime 之前才有效果哦
                        //System.out.println(calendarEnd.get(Calendar.YEAR) + "-" + (calendarEnd.get(Calendar.MONTH) + 1) + "-" + (calendarEnd.get(Calendar.DAY_OF_MONTH)));
                        dateEndPickerView.setEndTime(calendarEnd.get(Calendar.YEAR), calendarEnd.get(Calendar.MONTH) + 1,
                                calendarEnd.get(Calendar.DAY_OF_MONTH));
                        dateEndPickerView.setTime(new Date());
                        dateEndPickerView.setCyclic(false);
                        dateEndPickerView.setCancelable(true);
                        //时间选择后回调
                        dateEndPickerView.setOnTimeSelectListener(new DateEndPickerView.OnTimeSelectListener() {
                            @Override
                            public void onTimeSelect(Date date) {
                                tv_register_step_2_birthday.setText(getTime(date));
                            }
                        });
                        dateEndPickerView.show();
                    }
                }
            });
        }
        sexList.add(new SexBean("女", "0"));
        sexList.add(new SexBean("男", "1"));

        //长期有效状态改变的监听
        cb_register_step_2_id_time.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    tv_register_step_2_id_time.setText("");
                }
            }
        });

        tv_register_step_2.setTextColor(getResources().getColor(R.color.text_register_step_on));
        iv_register_step_2.setBackgroundResource(R.mipmap.icon_sign_in_step2);

        //邮箱焦点改变的监听
        et_register_step_2_emil.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (!hasFocus) {
                    if (!et_register_step_2_emil.getText().toString().trim().isEmpty()) {
                        if (!StringUtils.isEmail(et_register_step_2_emil.getText().toString().trim())) {
                            showConfirmAlert(RegisterStep2Activity.this, "邮箱格式不正确");
                        }
                    }
                }
            }
        });

        View borderViews[] = {et_register_step_2_duty, et_register_step_2_emil, et_register_step_2_postcode,
                et_register_step_2_address, et_tax_surname, et_tax_name, hwet_tax_live_address_province,
                hwet_tax_live_address_city, et_tax_live_chinese_address,
                hwet_tax_birthplace_address_city, et_others_control_name, et_control_identity, et_control_cause,
                et_beneficial_owner_name, et_bad_record, et_beneficial_owner_identity, et_register_step_2_duty,
                et_beneficial_owner_cause, et_tax_live_chinese_address,
                et_tax_live_pinyin_address, et_tax_live_english_address,
                hwet_tax_birthplace_address_province};

        ParamUtil.clearSelectedBorder(borderViews);
        setOnNextLinstener(new OnNextLinstener() {
            @Override
            public void OnCancelNext() {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(CUSTOMER_SERVICE_TELEPHONE_NUMBERS));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }

            @Override
            public void OnConfirmNext() {
                startActivity(new Intent().setClass(RegisterStep2Activity.this, LoginActivity.class));
                EventBus.getDefault().post(new RegisterSuccessEvent(true));
                finish();
            }

            @Override
            public void OndoConfirm() {

            }
        });
        et_register_step_2_address.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                String etRegisterAddress = et_register_step_2_address.getText().toString();
                if (!hasFocus) {
                    if (!StringUtils.isNullOrEmpty(etRegisterAddress)) {
                        if (etRegisterAddress.length() < 8) {
                            showConfirmAlert(RegisterStep2Activity.this,
                                    getResources().getString(R.string.string_register_step2_address_match1));
                            et_register_step_2_address.setIsSelected(true);
                            et_register_step_2_address.setText("");
                        } else if (!StringUtils.isContainChinese(etRegisterAddress)) {
                            showConfirmAlert(RegisterStep2Activity.this,
                                    getResources().getString(R.string.string_register_step2_address_match2));
                            et_register_step_2_address.setText("");
                            et_register_step_2_address.setIsSelected(true);
                        }
                    }
                }
            }
        });
    }

    /**
     * 新增纳税人的容器
     */
    private void initRecyclerViewLayout() {
        taxPayersContainer.addAll(generateData());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        linearLayoutManager.setSmoothScrollbarEnabled(true);
        linearLayoutManager.setAutoMeasureEnabled(true);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.addItemDecoration(new CustomDividerItemDecoration(DisplayUtil.dp2px(this, 10)));
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setNestedScrollingEnabled(false);
        taxPayerAdapter = new TaxPayerAdapter(taxPayersContainer);
        mRecyclerView.setAdapter(taxPayerAdapter);
        taxPayerAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        taxPayerAdapter.addHeaderView(getHeaderView());
        taxPayerAdapter.addFooterView(getFooterView());
        taxPayerAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                TaxPayerInfo adapterItemtem = (TaxPayerInfo) adapter.getItem(position);
                switch (adapterItemtem.getItemType()) {
                    case TaxPayerInfo.TAX_NO_OBTAIN:
                        switch (view.getId()) {
                            case R.id.tv_tax_noobtain_country:
                                tv_tax_ident_num_country = ((TextView) view);
                                currentType = position + 100;
                                currentPosition = 0;
                                initPopList();
                                if (list != null && !list.isEmpty() && list.size() > 0) {
                                    showPicker();
                                } else {
                                    getCountry(true);
                                }
                                break;
                            case R.id.ll_deleate_noobtain_taxpayer:
                                taxPayerAdapter.removeData(position);
                                taxPayersTotal.clear();
                                taxPayersTotal.addAll(taxPayersContainer);
                                break;
                        }
                        break;
                    case TaxPayerInfo.TAX_OBTAIN:
                        switch (view.getId()) {
                            case R.id.tv_tax_obtain_country:
                                tv_tax_ident_num_country = ((TextView) view);
                                currentType = position + 100;
                                currentPosition = 0;
                                initPopList();
                                if (list != null && !list.isEmpty() && list.size() > 0) {
                                    showPicker();
                                } else {
                                    getCountry(true);
                                }
                                break;
                            case R.id.ll_deleate_obtain_taxpayer:
                                taxPayerAdapter.removeData(position);
                                taxPayersTotal.clear();
                                taxPayersTotal.addAll(taxPayersContainer);
                                break;
                        }
                        break;
                }
            }
        });

        taxPayerAdapter.setOnViewCheckedChangeListener(new TaxPayerAdapter.OnViewCheckedChangeListener() {

            @Override
            public void OnCheckedChange(TaxPayerInfo taxPayerInfo, View view, BaseViewHolder helper, boolean isChecked) {
                int position = helper.getAdapterPosition();
                switch (taxPayerInfo.getItemType()) {
                    case TaxPayerInfo.TAX_NO_OBTAIN:
                        switch (view.getId()) {
                            case R.id.sbtn_tax_noobtain:
                                ((SwitchButton) view).setChecked(false);
                                taxPayerInfo.setTaxPayerSwitchStatus(true);
                                taxPayerInfo.setItemType(TaxPayerInfo.TAX_OBTAIN);
                                taxPayerAdapter.notifyItemChanged(position);
                                break;
                        }
                        break;
                    case TaxPayerInfo.TAX_OBTAIN:
                        switch (view.getId()) {
                            case R.id.sbtn_tax_obtain:
                                ((SwitchButton) view).setChecked(true);
                                taxPayerInfo.setTaxPayerSwitchStatus(false);
                                taxPayerInfo.setItemType(TaxPayerInfo.TAX_NO_OBTAIN);
                                taxPayerAdapter.notifyItemChanged(position);
                                break;
                        }
                        break;
                }
            }
        });
    }

    private List<TaxPayerInfo> generateData() {
        TaxPayerInfo payerInfo = new TaxPayerInfo();
        payerInfo.setItemType(TaxPayerInfo.TAX_OBTAIN);
        payerInfo.setTaxNum("");
        payerInfo.setTaxPosition("0");
        taxPayersTotal.add(payerInfo);
        return taxPayersTotal;
    }

    public void clearRecyclerView() {
        taxPayersContainer.clear();
        taxPayerAdapter.notifyDataSetChanged();
    }

    @NonNull
    private View getHeaderView() {
        //增加头
        View taxHeaderView = View.inflate(this, R.layout.include_register_step_2_head_layout, null);
        //初始化基本信息
        initBasicInfoLayout(taxHeaderView);
        //初始化由他人控制
        initAccountControlLayout(taxHeaderView);
        //初始化收益人
        initBeneficialOwnerLayout(taxHeaderView);
        //初始化不良记录
        initBedRecordLayout(taxHeaderView);

        //初始化涉税
        initTaxLayout(taxHeaderView);
        return taxHeaderView;
    }

    @NonNull
    private View getFooterView() {
        //增加尾
        View taxFooterView = View.inflate(this, R.layout.include_register_step_2_footer_layout, null);
        tv_other_taxpayer = (TextView) taxFooterView.findViewById(R.id.tv_other_taxpayer);
        tv_other_taxpayer.setOnClickListener(new FooterViewClickListener());
        taxFooterView.findViewById(R.id.btn_register_step_2).setOnClickListener(new FooterViewClickListener());
        cb_ident_shengming_info = (CheckBox) taxFooterView.findViewById(R.id.cb_ident_shengming_info);
        return taxFooterView;
    }

    private void initContentView() {
        rootview = (LinearLayout) findViewById(R.id.rootview);
        leftButton = (Button) findViewById(R.id.leftButton);
        title = (TextView) findViewById(R.id.title);
        rightButton = (Button) findViewById(R.id.rightButton);
        iv_register_step_2 = (ImageView) findViewById(R.id.iv_register_step_2);
        tv_register_step_2 = (TextView) findViewById(R.id.tv_register_step_2);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        leftButton.setOnClickListener(this);

    }

    /**
     * 初始化基本信息
     */
    private void initBasicInfoLayout(View taxHeaderView) {
        tv_register_step_2_id_time = (TextView) taxHeaderView.findViewById(R.id.tv_register_step_2_id_time); //有效期
        cb_register_step_2_id_time = (CheckBox) taxHeaderView.findViewById(R.id.cb_register_step_2_id_time); //有效期复选框
        tv_register_step_2_sex = (TextView) taxHeaderView.findViewById(R.id.tv_register_step_2_sex); //性别
        rl_register_step_2_occupation = (RelativeLayout) taxHeaderView.findViewById(R.id.rl_register_step_2_occupation);
        //职业
        tv_register_step_2_occupation = (TextView) taxHeaderView.findViewById(R.id.tv_register_step_2_occupation);
        et_register_step_2_duty = (HWEditText) taxHeaderView.findViewById(R.id.et_register_step_2_duty); //职务
        tv_register_step_2_birthday = (TextView) taxHeaderView.findViewById(R.id.tv_register_step_2_birthday); //出生日期
        et_register_step_2_emil = (HWEditText) taxHeaderView.findViewById(R.id.et_register_step_2_emil); //邮箱
        //居住地 省/市
        rl_register_step_2_province = (RelativeLayout) taxHeaderView.findViewById(R.id.rl_register_step_2_province);
        tv_register_step_2_province = (TextView) taxHeaderView.findViewById(R.id.tv_register_step_2_province);
        //居住地 市/区
        rl_register_step_2_city = (RelativeLayout) taxHeaderView.findViewById(R.id.rl_register_step_2_city);
        tv_register_step_2_city = (TextView) taxHeaderView.findViewById(R.id.tv_register_step_2_city);
        et_register_step_2_postcode = (HWEditText) taxHeaderView.findViewById(R.id.et_register_step_2_postcode);  //邮编
        et_register_step_2_address = (HWEditText) taxHeaderView.findViewById(R.id.et_register_step_2_address);  //详细地址
        et_register_step_2_id_tjr = (HWEditText) taxHeaderView.findViewById(R.id.et_register_step_2_id_tjr);  //推荐人
        iv_register_step_2_tjr = (ImageView) taxHeaderView.findViewById(R.id.iv_register_step_2_tjr);  //推荐人
        // TODO: 2018/5/3 309改为不可点击
//        tv_register_step_2_id_time.setOnClickListener(this);
//        cb_register_step_2_id_time.setOnClickListener(this);
        rl_register_step_2_occupation.setOnClickListener(this);
        rl_register_step_2_province.setOnClickListener(this);
        rl_register_step_2_city.setOnClickListener(this);
        iv_register_step_2_tjr.setOnClickListener(this);
        ParamUtil.setEditorNextAction(et_register_step_2_id_tjr);
    }

    /**
     * 初始化账户是否由他人控制的布局
     */
    private void initAccountControlLayout(View taxHeaderView) {
        sbtn_is_others_control = (SwitchButton) taxHeaderView.findViewById(R.id.sbtn_is_others_control); //控制开关
        ll_others_control_container = (LinearLayout) taxHeaderView.findViewById(R.id.ll_others_control_container);
        et_others_control_name = (HWEditText) taxHeaderView.findViewById(R.id.et_others_control_name); //真实姓名
        tv_control_identityType = (TextView) taxHeaderView.findViewById(R.id.tv_control_identityType); //证件类型
        et_control_identity = (HWEditText) taxHeaderView.findViewById(R.id.et_control_identity); //证件号
        iv_control_identityType = (ImageView) taxHeaderView.findViewById(R.id.iv_control_identityType);
        tv_control_identity_validity = (TextView) taxHeaderView.findViewById(R.id.tv_control_identity_validity);
        cb_control_identity_validity_time = (CheckBox) taxHeaderView.findViewById(R.id.cb_control_identity_validity_time);
        et_control_cause = (HWEditText) taxHeaderView.findViewById(R.id.et_control_cause);
        tv_control_length = (TextView) taxHeaderView.findViewById(R.id.tv_control_length);

        tv_control_identityType.setOnClickListener(this);
        iv_control_identityType.setOnClickListener(this);
        tv_control_identity_validity.setOnClickListener(this);

        sbtn_is_others_control.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked) {
                    ll_others_control_container.setVisibility(View.VISIBLE);
                } else {
                    ll_others_control_container.setVisibility(View.GONE);
                }
            }
        });
        et_control_cause.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                MentorLogger.Loge(TAG, "charSequence = " + charSequence + ", i=" + i + ", i1=" + i1 + " ,i2=" + i2);
                if (!StringUtils.isNullOrEmpty(charSequence.toString()) && charSequence.toString().length() > 0) {
                    if (charSequence.toString().length() > 120) {
                        tv_control_length.setText("120/120字");
                    } else {
                        tv_control_length.setText(charSequence.toString().length() + "/120字");
                    }
                } else {
                    tv_control_length.setText("0/120字");
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
                String string = editable.toString();
                MentorLogger.Loge(TAG, "afterTextChanged" + string.length());

            }
        });
        ParamUtil.setEditorNextAction(et_control_cause);
        cb_control_identity_validity_time.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked) {
                    tv_control_identity_validity.setText("");
                }
            }
        });
    }

    /**
     * 初始化涉税布局
     */
    private void initTaxLayout(View taxHeaderView) {
        ll_tax_layout = (LinearLayout) taxHeaderView.findViewById(R.id.ll_tax_layout);
        tv_register_step_2_identity_statement = (TextView) taxHeaderView.findViewById(R.id.tv_register_step_2_identity_statement);
        iv_register_step_2_identity_statement = (ImageView) taxHeaderView.findViewById(R.id.iv_register_step_2_identity_statement);
        rl_tips_flag = (RelativeLayout) taxHeaderView.findViewById(R.id.rl_tips_flag);
        tv_tips_flag = (TextView) taxHeaderView.findViewById(R.id.tv_tips_flag);
        tv_register_step_2_identity_statement.setOnClickListener(this);
        iv_register_step_2_identity_statement.setOnClickListener(this);
        rl_tips_flag.setOnClickListener(this);
        //税收居民
        ll_tax_user_info_layout = (LinearLayout) taxHeaderView.findViewById(R.id.ll_tax_user_info_layout);
        et_tax_surname = (HWEditText) taxHeaderView.findViewById(R.id.et_tax_surname);
        et_tax_name = (HWEditText) taxHeaderView.findViewById(R.id.et_tax_name);
        et_tax_surname.addTextChangedListener(new InputTextWatcher(et_tax_surname, "姓的输入长度为60"));
        et_tax_name.addTextChangedListener(new InputTextWatcher(et_tax_name, "名的输入长度为60"));
        //现居住详细地址
        ll_tax_live_address_layout = (LinearLayout) taxHeaderView.findViewById(R.id.ll_tax_live_address_layout);
        //国家
        rl_tv_tax_live_address_country_layout = (RelativeLayout) taxHeaderView.findViewById(R.id.rl_tv_tax_live_address_country_layout);
        tv_tax_live_address_country = (TextView) taxHeaderView.findViewById(R.id.tv_tax_live_address_country);
        rl_tv_tax_live_address_country_layout.setOnClickListener(this);
        //省
        rl_tax_live_address_province_layout = (RelativeLayout) taxHeaderView.findViewById(R.id.rl_tax_live_address_province_layout);
        tv_tax_live_address_province = (TextView) taxHeaderView.findViewById(R.id.tv_tax_live_address_province);
        hwet_tax_live_address_province = (HWEditText) taxHeaderView.findViewById(R.id.hwet_tax_live_address_province);
        iv_tax_live_address_province = (ImageView) taxHeaderView.findViewById(R.id.iv_tax_live_address_province);
        rl_tax_live_address_province_layout.setOnClickListener(this);
        //市
        rl_tax_live_address_city_layout = (RelativeLayout) taxHeaderView.findViewById(R.id.rl_tax_live_address_city_layout);
        tv_tax_live_address_city = (TextView) taxHeaderView.findViewById(R.id.tv_tax_live_address_city);
        hwet_tax_live_address_city = (HWEditText) taxHeaderView.findViewById(R.id.hwet_tax_live_address_city);
        iv_tax_live_address_city = (ImageView) taxHeaderView.findViewById(R.id.iv_tax_live_address_city);
        rl_tax_live_address_city_layout.setOnClickListener(this);
        //中文地址
        ll_live_chinese_address_container = (LinearLayout) taxHeaderView.findViewById(R.id.ll_live_chinese_address_container);
        et_tax_live_chinese_address = (HWEditText) taxHeaderView.findViewById(R.id.et_tax_live_chinese_address);
        et_tax_live_pinyin_address = (HWEditText) taxHeaderView.findViewById(R.id.et_tax_live_pinyin_address);
        et_tax_live_chinese_address.addTextChangedListener(new InputTextWatcher(et_tax_live_chinese_address,
                "中文地址的输入长度为60"));
        et_tax_live_pinyin_address.addTextChangedListener(new InputTextWatcher(et_tax_live_pinyin_address,
                "拼音地址的输入长度为60"));
        //非中文地址
        ll_live_nochinese_address_container = (LinearLayout) taxHeaderView.findViewById(R.id.ll_live_nochinese_address_container);
        et_tax_live_english_address = (HWEditText) taxHeaderView.findViewById(R.id.et_tax_live_english_address);
        et_tax_live_english_address.addTextChangedListener(new InputTextWatcher(et_tax_live_english_address,
                "英文地址的输入长度为60"));
        //出生地详细地址
        ll_tax_birthplace_address_layout = (LinearLayout) taxHeaderView.findViewById(R.id.ll_tax_birthplace_address_layout);
        //国家
        rl_tax_birthplace_address_country = (RelativeLayout) taxHeaderView.findViewById(R.id.rl_tax_birthplace_address_country);
        tv_tax_birthplace_address_country = (TextView) taxHeaderView.findViewById(R.id.tv_tax_birthplace_address_country);
        rl_tax_birthplace_address_country.setOnClickListener(this);
        //省
        rl_tax_birthplace_address_province_layout = (RelativeLayout) taxHeaderView.findViewById(R.id.rl_tax_birthplace_address_province_layout);
        tv_tax_birthplace_address_province = (TextView) taxHeaderView.findViewById(R.id.tv_tax_birthplace_address_province);
        hwet_tax_birthplace_address_province = (HWEditText) taxHeaderView.findViewById(R.id.hwet_tax_birthplace_address_province);
        iv_tax_birthplace_address_province = (ImageView) taxHeaderView.findViewById(R.id.iv_tax_birthplace_address_province);
        rl_tax_birthplace_address_province_layout.setOnClickListener(this);
        //市
        rl_tax_birthplace_address_city_layout = (RelativeLayout) taxHeaderView.findViewById(R.id.rl_tax_birthplace_address_city_layout);
        tv_tax_birthplace_address_city = (TextView) taxHeaderView.findViewById(R.id.tv_tax_birthplace_address_city);
        hwet_tax_birthplace_address_city = (HWEditText) taxHeaderView.findViewById(R.id.hwet_tax_birthplace_address_city);
        iv_tax_birthplace_address_city = (ImageView) taxHeaderView.findViewById(R.id.iv_tax_birthplace_address_city);
        rl_tax_birthplace_address_city_layout.setOnClickListener(this);
    }

    /**
     * 初始化不良记录的布局
     */
    private void initBedRecordLayout(View taxHeaderView) {
        sbtn_is_bad_record = (SwitchButton) taxHeaderView.findViewById(R.id.sbtn_is_bad_record);
        ll_bad_record_container = (LinearLayout) taxHeaderView.findViewById(R.id.ll_bad_record_container);
        et_bad_record = (HWEditText) taxHeaderView.findViewById(R.id.et_bad_record);
        tv_bad_record_length = (TextView) taxHeaderView.findViewById(R.id.tv_bad_record_length);

        sbtn_is_bad_record.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked) {
                    ll_bad_record_container.setVisibility(View.VISIBLE);
                } else {
                    ll_bad_record_container.setVisibility(View.GONE);
                }
            }
        });
        ParamUtil.setEditorNextAction(et_bad_record);
        et_bad_record.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (!StringUtils.isNullOrEmpty(charSequence.toString()) && charSequence.toString().length() > 0) {
                    if (charSequence.toString().length() > 120) {
                        tv_bad_record_length.setText("120/120字");
                    } else {
                        tv_bad_record_length.setText(charSequence.toString().length() + "/120字");
                    }
                } else {
                    tv_bad_record_length.setText("0/120字");
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    /**
     * 初始化受益人布局
     */
    private void initBeneficialOwnerLayout(View taxHeaderView) {
        sbtn_is_beneficial_owner = (SwitchButton) taxHeaderView.findViewById(R.id.sbtn_is_beneficial_owner);
        ll_beneficial_owner_container = (LinearLayout) taxHeaderView.findViewById(R.id.ll_beneficial_owner_container);
        et_beneficial_owner_name = (HWEditText) taxHeaderView.findViewById(R.id.et_beneficial_owner_name);
        et_beneficial_owner_identity = (HWEditText) taxHeaderView.findViewById(R.id.et_beneficial_owner_identity);
        tv_beneficial_identityType = (TextView) taxHeaderView.findViewById(R.id.tv_beneficial_identityType);
        iv_beneficial_identityType = (ImageView) taxHeaderView.findViewById(R.id.iv_beneficial_identityType);
        tv_beneficial_identity_validity = (TextView) taxHeaderView.findViewById(R.id.tv_beneficial_identity_validity);
        cb_beneficial_identity_validity_time = (CheckBox) taxHeaderView.findViewById(R.id.cb_beneficial_identity_validity_time);
        et_beneficial_owner_cause = (HWEditText) taxHeaderView.findViewById(R.id.et_beneficial_owner_cause);
        tv_beneficial_owner_length = (TextView) taxHeaderView.findViewById(R.id.tv_beneficial_owner_length);

        tv_beneficial_identityType.setOnClickListener(this);
        iv_beneficial_identityType.setOnClickListener(this);
        tv_beneficial_identity_validity.setOnClickListener(this);

        this.sbtn_is_beneficial_owner.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked) {
                    ll_beneficial_owner_container.setVisibility(View.GONE);
                } else {
                    ll_beneficial_owner_container.setVisibility(View.VISIBLE);
                }
            }
        });

        et_beneficial_owner_cause.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (!StringUtils.isNullOrEmpty(charSequence.toString()) && charSequence.toString().length() > 0) {
                    if (charSequence.toString().length() > 120) {
                        tv_beneficial_owner_length.setText("120/120字");
                    } else {
                        tv_beneficial_owner_length.setText(charSequence.toString().length() + "/120字");
                    }
                } else {
                    tv_beneficial_owner_length.setText("0/120字");
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        ParamUtil.setEditorNextAction(et_beneficial_owner_cause);

        cb_beneficial_identity_validity_time.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked) {
                    tv_beneficial_identity_validity.setText("");
                }
            }
        });
        if (identityTypeList != null && !identityTypeList.isEmpty() && identityTypeList.size() > 0) {
        } else {
            getIndentityType(false);
        }
    }

    /**
     * 获取证件类型
     *
     * @param isShowPop
     */
    private void getIndentityType(final boolean isShowPop) {
        Map<String, String> params = new HashMap<>();
        params.put("type", "1");
        OkGo.<String>post(HostConstant.QUERY_CERTIFICATE_TYPE)
                .tag(this)
                .params(HttpUtils.getCommonParams(this, params))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        UpdateIdType(response.body(), isShowPop);
                    }


                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        showToast("网络加载失败，请稍后再试!");
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }
                });
    }

    private void UpdateIdType(String json, boolean isShowPop) {
        identityTypeList.clear();

        QueryCertificateTypeBean bean;
        try {
            bean = new Gson().fromJson(json, QueryCertificateTypeBean.class);
        } catch (Exception exception) {
            showConfirmAlert(mActivity, "提示", "数据解析错误");
            return;
        }

        if (!CommonUtil.checkState(mActivity, bean, json)) {
            return;
        }

        if (bean == null || bean.getData() == null ||
                bean.getData().isEmpty() || bean.getData().size() < 1) {
            showConfirmAlert(mActivity, "提示", getString(R.string.no_data));
            return;
        }

        identityTypeList.addAll(bean.getData());
        initPopList();

        if (isFirstComing) {
            isFirstComing = false;
            selectPosition = 0;
            tv_control_identityType.setText(identityTypeList.get(selectPosition).getTypeName().length() > 4 ?
                    identityTypeList.get(selectPosition).getTypeName().substring(0, 4) :
                    identityTypeList.get(selectPosition).getTypeName());
        }

        if (isShowPop) {
            showPicker();
        }
    }

    public String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    /**
     * 职业查询
     *
     * @param isShowPop
     */
    private void getProfession(final boolean isShowPop) {
        OkGo.<String>post(HostConstant.QUERY_PROFESSION)
                .tag(this)
                .params(HttpUtils.getCommonParams(this, null))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        UpdateProfession(response.body(), isShowPop);
                    }


                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        showToast("网络加载失败，请稍后再试!");
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }
                });
    }

    /**
     * 职业数据解析
     *
     * @param json
     * @param isShowPop
     */
    private void UpdateProfession(String json, boolean isShowPop) {
        professionList.clear();
        list.clear();

        QueryProfessionBean bean;
        try {
            bean = new Gson().fromJson(json, QueryProfessionBean.class);
        } catch (Exception exception) {
            showConfirmAlert(mActivity, "提示", "数据解析错误");
            return;
        }

        if (!CommonUtil.checkState(mActivity, bean, json)) {
            return;
        }

        if (bean == null || bean.getData() == null ||
                bean.getData().isEmpty() || bean.getData().size() < 1) {
            showConfirmAlert(mActivity, "提示", getString(R.string.no_data));
            return;
        }

        professionList.addAll(bean.getData());
        initPopList();

        if (isShowPop) {
            showPicker();
        }
    }

    /**
     * 查询涉税的省
     *
     * @param isShowPop
     */
    private void getTaxProvince(final boolean isShowPop) {
        OkGo.<String>post(HostConstant.QUERY_TAX_PROVINCE)
                .tag(this)
                .upJson(new Gson().toJson(HttpUtils.getCommonParams(this, null)))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        UpdateTaxProvince(response.body(), isShowPop);
                    }


                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        showToast("网络加载失败，请稍后再试!");
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }
                });
    }

    private void UpdateTaxProvince(String json, boolean isShowPop) {
        QueryTaxProvinceCityBean bean;
        try {
            bean = new Gson().fromJson(json, QueryTaxProvinceCityBean.class);
        } catch (Exception exception) {
            showConfirmAlert(mActivity, "提示", "数据解析错误");
            return;
        }

        if (bean == null || bean.getData() == null ||
                bean.getData().isEmpty() || bean.getData().size() < 1) {
            showConfirmAlert(mActivity, "提示", getString(R.string.no_data));
            return;
        }

        list.clear();
        taxProvinceList.clear();
        taxProvinceList.addAll(bean.getData());
        initPopList();
        if (isShowPop) {
            showPicker();
        }
    }

    /**
     * 查询涉税的市
     *
     * @param isShowPop
     */
    private void getTaxCity(String provinceCode, final boolean isShowPop) {
        Map<String, String> params = new HashMap<>();
        params.put("provinceCode", provinceCode);
        OkGo.<String>post(HostConstant.QUERY_TAX_CITY)
                .tag(this)
                .upJson(new Gson().toJson(HttpUtils.getCommonParams(this, params)))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        UpdateTaxCity(response.body(), isShowPop);
                    }


                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        showToast("网络加载失败，请稍后再试!");
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }
                });
    }

    private void UpdateTaxCity(String json, boolean isShowPop) {
        QueryTaxProvinceCityBean bean;
        try {
            bean = new Gson().fromJson(json, QueryTaxProvinceCityBean.class);
        } catch (Exception exception) {
            showConfirmAlert(mActivity, "提示", "数据解析错误");
            return;
        }

        if (bean == null || bean.getData() == null ||
                bean.getData().isEmpty() || bean.getData().size() < 1) {
            showConfirmAlert(mActivity, "提示", getString(R.string.no_data));
            return;
        }

        list.clear();
        taxCityList.clear();
        taxCityList.addAll(bean.getData());
        initPopList();
        if (isShowPop) {
            showPicker();
        }
    }


    /**
     * @param queryType    0-省，1-市
     * @param provinceCode 查市时必传
     * @param isShowPop
     */
    private void getProvinceCity(final int queryType, String provinceCode, final boolean isShowPop) {
        Map<String, String> params = new HashMap<>();
        params.put("queryType", queryType + "");
        if (queryType == 1) {
            params.put("provinceCode", provinceCode);
        }
        OkGo.<String>post(HostConstant.QUERY_PROVINCE_CITY)
                .tag(this)
                .params(HttpUtils.getCommonParams(this, params))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        UpdateProvinceCity(response.body(), queryType, isShowPop);
                    }


                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        showToast("网络加载失败，请稍后再试!");
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }
                });
    }


    private void UpdateProvinceCity(String json, int queryType, boolean isShowPop) {

        QueryProvinceCityBean bean;
        try {
            bean = new Gson().fromJson(json, QueryProvinceCityBean.class);
        } catch (Exception exception) {
            showConfirmAlert(mActivity, "提示", "数据解析错误");
            return;
        }

        if (!CommonUtil.checkState(mActivity, bean, json)) {
            return;
        }

        if (bean == null || bean.getData() == null ||
                bean.getData().isEmpty() || bean.getData().size() < 1) {
            showConfirmAlert(mActivity, "提示", getString(R.string.no_data));
            return;
        }

        list.clear();

        switch (queryType) {
            case 0:
                provinceList.clear();
                provinceList.addAll(bean.getData());

                initPopList();

                if (isShowPop) {
                    showPicker();
                }
                break;
            case 1:
                cityList.clear();
                cityList.addAll(bean.getData());

                initPopList();

                if (isShowPop) {
                    showPicker();
                }
                break;
            default:
                break;
        }
    }

    private void showPicker() {
        nomarlPickerPopupWindow = new NomarlPickerPopupWindow(mActivity, itemsOnClick, list);
        nomarlPickerPopupWindow.showAtLocation(findViewById(R.id.rootview),
                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        switch (currentType) {
            case 1:
                nomarlPickerPopupWindow.setTitle_str("请选择您的性别");
                break;
            case 2:
                nomarlPickerPopupWindow.setTitle_str("请选择您的职业");
                break;
            case 3:
            case 11:
            case 13:
                nomarlPickerPopupWindow.setTitle_str("请选择您所在的省");
                break;
            case 4:
            case 12:
            case 14:
                nomarlPickerPopupWindow.setTitle_str("请选择您所在的市");
                break;
            case 5:
                nomarlPickerPopupWindow.setTitle_str("请选择居民身份类型");
                break;
            case 6:
            case 7:
            case 100:
            case 101:
            case 102:
            case 103:
            case 104:
            case 105:
            case 106:
            case 107:
            case 108:
            case 109:
                nomarlPickerPopupWindow.setTitle_str("请选择国别或地区");
                break;
            case 15:
                nomarlPickerPopupWindow.setTitle_str("请选择他人控制身份证");
                break;
            case 16:
                nomarlPickerPopupWindow.setTitle_str("请选择受益人身份证");
                break;
            default:
                break;
        }

        nomarlPickerPopupWindow.setOnPickerChangeListener(
                new NomarlPickerPopupWindow.OnPickerChangeListener() {
                    @Override
                    public void onPickerChange(int position) {
                        currentPosition = position;
                    }
                });

    }

    //为弹出窗口实现监听类
    private View.OnClickListener itemsOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            nomarlPickerPopupWindow.dismiss();
            switch (v.getId()) {
                case R.id.btn_cancel:
                    if (nomarlPickerPopupWindow != null) {
                        nomarlPickerPopupWindow.dismiss();
                    }
                    break;
                case R.id.btn_sure:
                    if (nomarlPickerPopupWindow != null) {
                        selectPosition = currentPosition;
                        nomarlPickerPopupWindow.dismiss();

                        switch (currentType) {
                            case 1:
                                tv_register_step_2_sex.setText(list.get(selectPosition));
                                sex = sexList.get(selectPosition).getSexId();
                                break;
                            case 2:
                                tv_register_step_2_occupation.setText(list.get(selectPosition));
                                profession = professionList.get(selectPosition).getProfessionCode();
                                break;
                            case 3:
                                tv_register_step_2_province.setText(list.get(selectPosition));
                                if (selectedProvince != provinceList.get(selectPosition).getProvinceCode()) {
                                    tv_register_step_2_city.setText("");
                                    cityCode = "";
                                }
                                selectedProvince = provinceList.get(selectPosition).getProvinceCode();
                                provinceCode = provinceList.get(selectPosition).getProvinceCode();
                                break;
                            case 4:
                                tv_register_step_2_city.setText(list.get(selectPosition));
                                cityCode = cityList.get(selectPosition).getCityCode();
                                break;
                            case 5:
                                String oldStatement = tv_register_step_2_identity_statement.getText().toString();
                                tv_register_step_2_identity_statement.setText(list.get(selectPosition));
                                identStatement = taxTypeList.get(selectPosition).getItemValue();
                                //为了解决重复选择仅中国税收居民将第一条其他纳税人删除
                                if (!oldStatement.equals(list.get(selectPosition))) {
                                    taxLayoutShow(identStatement);
                                }
                                break;
                            case 6:
                                tv_tax_live_address_country.setText(list.get(selectPosition));
                                if (liveAddressCountryId != countryList.get(selectPosition).getItemValue()) {
                                    tv_tax_live_address_province.setText("");
                                    hwet_tax_live_address_province.setText("");
                                    tv_tax_live_address_city.setText("");
                                    hwet_tax_live_address_city.setText("");
                                    liveAddressProvinceCode = "";
                                    liveAddressCityCode = "";
                                }
                                liveAddressCountryId = countryList.get(selectPosition).getItemValue();
                                if (isSelectTaxCountry(liveAddressCountryId)) { //是中国
                                    hwet_tax_live_address_province.setVisibility(View.GONE);
                                    tv_tax_live_address_province.setVisibility(View.VISIBLE);
                                    iv_tax_live_address_province.setVisibility(View.VISIBLE);

                                    hwet_tax_live_address_city.setVisibility(View.GONE);
                                    tv_tax_live_address_city.setVisibility(View.VISIBLE);
                                    iv_tax_live_address_city.setVisibility(View.VISIBLE);

                                    ll_live_chinese_address_container.setVisibility(View.VISIBLE);
                                    ll_live_nochinese_address_container.setVisibility(View.GONE);
                                } else {
                                    hwet_tax_live_address_province.setVisibility(View.VISIBLE);
                                    tv_tax_live_address_province.setVisibility(View.INVISIBLE);
                                    iv_tax_live_address_province.setVisibility(View.INVISIBLE);

                                    hwet_tax_live_address_city.setVisibility(View.VISIBLE);
                                    tv_tax_live_address_city.setVisibility(View.INVISIBLE);
                                    iv_tax_live_address_city.setVisibility(View.INVISIBLE);

                                    ll_live_nochinese_address_container.setVisibility(View.VISIBLE);
                                    ll_live_chinese_address_container.setVisibility(View.GONE);
                                }
                                break;
                            case 7:
                                tv_tax_birthplace_address_country.setText(list.get(selectPosition));
                                if (birthAddressCountryId != countryList.get(selectPosition).getItemValue()) {
                                    tv_tax_birthplace_address_province.setText("");
                                    tv_tax_birthplace_address_city.setText("");
                                    birthAddressProvinceCode = "";
                                    birthAddressCityCode = "";
                                }
                                birthAddressCountryId = countryList.get(selectPosition).getItemValue();
                                if (isSelectTaxCountry(birthAddressCountryId)) {//是中国则省市可选,否则需要输入
                                    hwet_tax_birthplace_address_province.setVisibility(View.GONE);
                                    tv_tax_birthplace_address_province.setVisibility(View.VISIBLE);
                                    iv_tax_birthplace_address_province.setVisibility(View.VISIBLE);

                                    hwet_tax_birthplace_address_city.setVisibility(View.GONE);
                                    tv_tax_birthplace_address_city.setVisibility(View.VISIBLE);
                                    iv_tax_birthplace_address_city.setVisibility(View.VISIBLE);
                                } else {
                                    hwet_tax_birthplace_address_province.setVisibility(View.VISIBLE);
                                    tv_tax_birthplace_address_province.setVisibility(View.GONE);
                                    iv_tax_birthplace_address_province.setVisibility(View.GONE);

                                    hwet_tax_birthplace_address_city.setVisibility(View.VISIBLE);
                                    tv_tax_birthplace_address_city.setVisibility(View.GONE);
                                    iv_tax_birthplace_address_city.setVisibility(View.GONE);
                                }
                                break;
                            case 11:
                                tv_tax_live_address_province.setText(list.get(selectPosition));
                                if (liveAddressProvinceCode != getSelectProvinceCode(liveAddressCountryId, selectPosition)) {
                                    tv_tax_live_address_city.setText("");
                                    liveAddressCityCode = "";
                                }
                                liveAddressProvinceCode = getSelectProvinceCode(liveAddressCountryId, selectPosition);
                                break;
                            case 12:
                                tv_tax_live_address_city.setText(list.get(selectPosition));
                                liveAddressCityCode = taxCityList.get(selectPosition).getRegionCode();
                                break;
                            case 13:
                                tv_tax_birthplace_address_province.setText(list.get(selectPosition));
                                if (birthAddressProvinceCode != getSelectProvinceCode(birthAddressCountryId, selectPosition)) {
                                    tv_tax_birthplace_address_city.setText("");
                                    birthAddressCityCode = "";
                                }
                                birthAddressProvinceCode = getSelectProvinceCode(birthAddressCountryId, selectPosition);
                                break;
                            case 14:
                                tv_tax_birthplace_address_city.setText(list.get(selectPosition));
                                birthAddressCityCode = taxCityList.get(selectPosition).getRegionCode();
                                break;
                            case 15:
                                tv_control_identityType.setText(list.get(selectPosition));
                                controllerCertType = identityTypeList.get(selectPosition).getTypeValue();
                                break;
                            case 16:
                                tv_beneficial_identityType.setText(list.get(selectPosition));
                                beneficiaryCertType = identityTypeList.get(selectPosition).getTypeValue();
                                break;
                            //以下为其他纳税人选择的识别国
                            case 100:
                                tv_tax_ident_num_country.setText(list.get(selectPosition));
                                taxPayersTotal.get(0).setTaxPayerCountry(list.get(selectPosition));
                                taxPayersTotal.get(0).setTaxPayerCountryId(countryList.get(selectPosition).getItemValue());
                                break;
                            case 101:
                                tv_tax_ident_num_country.setText(list.get(selectPosition));
                                taxPayersTotal.get(1).setTaxPayerCountry(list.get(selectPosition));
                                taxPayersTotal.get(1).setTaxPayerCountryId(countryList.get(selectPosition).getItemValue());
                                break;
                            case 102:
                                tv_tax_ident_num_country.setText(list.get(selectPosition));
                                taxPayersTotal.get(2).setTaxPayerCountry(list.get(selectPosition));
                                taxPayersTotal.get(2).setTaxPayerCountryId(countryList.get(selectPosition).getItemValue());
                                break;
                            case 103:
                                tv_tax_ident_num_country.setText(list.get(selectPosition));
                                taxPayersTotal.get(3).setTaxPayerCountry(list.get(selectPosition));
                                taxPayersTotal.get(3).setTaxPayerCountryId(countryList.get(selectPosition)
                                        .getItemValue());
                                break;
                            case 104:
                                tv_tax_ident_num_country.setText(list.get(selectPosition));
                                taxPayersTotal.get(4).setTaxPayerCountry(list.get(selectPosition));
                                taxPayersTotal.get(4).setTaxPayerCountryId(countryList.get(selectPosition)
                                        .getItemValue());
                                break;
                            case 105:
                                tv_tax_ident_num_country.setText(list.get(selectPosition));
                                taxPayersTotal.get(5).setTaxPayerCountry(list.get(selectPosition));
                                taxPayersTotal.get(5).setTaxPayerCountryId(countryList.get(selectPosition)
                                        .getItemValue());
                                break;
                            case 106:
                                tv_tax_ident_num_country.setText(list.get(selectPosition));
                                taxPayersTotal.get(6).setTaxPayerCountry(list.get(selectPosition));
                                taxPayersTotal.get(6).setTaxPayerCountryId(countryList.get(selectPosition)
                                        .getItemValue());
                                break;
                            case 107:
                                tv_tax_ident_num_country.setText(list.get(selectPosition));
                                taxPayersTotal.get(7).setTaxPayerCountry(list.get(selectPosition));
                                taxPayersTotal.get(7).setTaxPayerCountryId(countryList.get(selectPosition)
                                        .getItemValue());
                                break;
                            case 108:
                                tv_tax_ident_num_country.setText(list.get(selectPosition));
                                taxPayersTotal.get(8).setTaxPayerCountry(list.get(selectPosition));
                                taxPayersTotal.get(8).setTaxPayerCountryId(countryList.get(selectPosition)
                                        .getItemValue());
                                break;
                            case 109:
                                tv_tax_ident_num_country.setText(list.get(selectPosition));
                                taxPayersTotal.get(9).setTaxPayerCountry(list.get(selectPosition));
                                taxPayersTotal.get(9).setTaxPayerCountryId(countryList.get(selectPosition)
                                        .getItemValue());
                                break;
                            default:
                                break;
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    };

    private void initPopList() {
        list.clear();

        switch (currentType) {
            case 1:
                for (SexBean dataBean : sexList) {
                    list.add(dataBean.getSexName());
                }
                break;
            case 2:
                for (QueryProfessionBean.DataBean dataBean : professionList) {
                    list.add(dataBean.getProfessionName());
                }
                break;
            case 3:
                for (QueryProvinceCityBean.DataBean dataBean : provinceList) {
                    list.add(dataBean.getProvinceName());
                }
                break;
            case 4:
                for (QueryProvinceCityBean.DataBean dataBean : cityList) {
                    list.add(dataBean.getCityName());
                }
                break;
            //涉税的省数据
            case 11:
            case 13:
                for (QueryTaxProvinceCityBean.DataBean dataBean : taxProvinceList) {
                    list.add(dataBean.getRegionNameCn());
                }
                break;
            //涉税的市数据
            case 12:
            case 14:
                for (QueryTaxProvinceCityBean.DataBean dataBean : taxCityList) {
                    list.add(dataBean.getRegionNameCn());
                }
                break;
            case 5:
                for (QueryCountryBean.DataBean dataBean : taxTypeList) {
                    list.add(dataBean.getItemName());
                }
                break;
            case 6:
            case 7:
            case 100:
            case 101:
            case 102:
            case 103:
            case 104:
            case 105:
            case 106:
            case 107:
            case 108:
            case 109:
                for (QueryCountryBean.DataBean dataBean : countryList) {
                    list.add(dataBean.getItemName());
                }
                break;
            case 15:
            case 16:
                for (QueryCertificateTypeBean.DataBean dataBean : identityTypeList) {
                    list.add(dataBean.getTypeName());
                }
                break;

            default:
                break;
        }

    }

    @OnClick({R.id.leftButton, R.id.cb_register_step_2_id_time, R.id.tv_register_step_2_id_time,
            R.id.rl_register_step_2_occupation, R.id.rl_register_step_2_province, R.id.rl_register_step_2_city,
            R.id.iv_register_step_2_tjr, R.id.tv_register_step_2_identity_statement, R.id.iv_register_step_2_identity_statement,
            R.id.rl_tv_tax_live_address_country_layout, R.id.rl_tax_birthplace_address_country,
            R.id.rl_tax_live_address_province_layout, R.id.rl_tax_live_address_city_layout, R.id.rl_tips_flag,
            R.id.rl_tax_birthplace_address_province_layout, R.id.rl_tax_birthplace_address_city_layout,
            R.id.tv_control_identityType, R.id.iv_control_identityType, R.id.tv_control_identity_validity,
            R.id.tv_beneficial_identity_validity, R.id.tv_beneficial_identityType, R.id.iv_beneficial_identityType})
    public void onClick(View view) {
        if (!isFastDoubleClick()) {
            collapseSoftInputMethod(view.getWindowToken());
            switch (view.getId()) {
                case R.id.leftButton:
                    finish();
                    break;
                case R.id.rl_tips_flag: //3.0.8新增解释入口popwindow
                    TipPopwindowHelper popwindowHelper = TipPopwindowHelper.getInstance(this);
                    popwindowHelper.initPopwindow();
                    if ("OF".equals(identStatement)) { //仅非居民(OF) 3：即使中国税收居民又是他国税收居民(NF)
                        popwindowHelper.setTextContent(getString(R.string.tips_no_tax_man));
                    } else if ("ON".equals(identStatement)) {
                        popwindowHelper.setTextContent(getString(R.string.tips_tax_man));
                    }
                    popwindowHelper.showPopLocation(tv_tips_flag);
                    break;
                case R.id.cb_register_step_2_id_time://开户者证件有效期复选框
                    break;
                case R.id.tv_register_step_2_id_time: //开户者证件有效期
//
                    datePickerType = 100;
                    showDatePickerView();
                    break;
                case R.id.iv_control_identityType: //账户他人控制点击身份证
                case R.id.tv_control_identityType: //账户他人控制点击身份证
                    currentType = 15;
                    currentPosition = 0;
                    initPopList();
                    if (list != null && !list.isEmpty() && list.size() > 0) {
                        showPicker();
                    } else {
                        getIndentityType(true);
                    }
                    break;
                case R.id.tv_beneficial_identityType: //受益人点击身份证
                case R.id.iv_beneficial_identityType: //受益人点击身份证
                    currentType = 16;
                    currentPosition = 0;
                    initPopList();
                    if (list != null && !list.isEmpty() && list.size() > 0) {
                        showPicker();
                    } else {
                        getIndentityType(true);
                    }
                    break;
                case R.id.tv_control_identity_validity: //账户他人证件有效期
                    datePickerType = 101;
                    showDatePickerView();
                    break;
                case R.id.tv_beneficial_identity_validity: //受益人证件有效期
                    datePickerType = 102;
                    showDatePickerView();
                    break;
                case R.id.rl_register_step_2_occupation: //点击职业
                    currentType = 2;
                    currentPosition = 0;
                    initPopList();
                    if (list != null && !list.isEmpty() && list.size() > 0) {
                        showPicker();
                    } else {
                        getProfession(true);
                    }
                    break;
                case R.id.rl_register_step_2_province:
                    currentType = 3;
                    currentPosition = 0;
                    initPopList();

                    if (list != null && !list.isEmpty() && list.size() > 0) {
                        showPicker();
                    } else {
                        getProvinceCity(0, "", true);
                    }
                    break;
                case R.id.rl_register_step_2_city:
                    if (TextUtils.isEmpty(selectedProvince)) {
                        showToast("请先选择您所在的省");
                        return;
                    }
                    currentType = 4;
                    currentPosition = 0;
                    cityList.clear();
                    initPopList();

                    if (list != null && !list.isEmpty() && list.size() > 0) {
                        showPicker();
                    } else {
                        getProvinceCity(1, selectedProvince, true);
                    }

                    break;

                case R.id.iv_register_step_2_tjr:
                    if (PermissionManager.getInstance().checkPermissions(this, PermissionManager.PERMISSION_READ_WEITE_CAMERA)) {
                        Intent it = new Intent(RegisterStep2Activity.this, CaptureActivity.class);
                        startActivityForResult(it, CAPTURE);
                    } else {
                        PermissionManager.getInstance().requestPermissions(this, PermissionManager.REQUEST_READ_WEITE_CAMERA,
                                PermissionManager.PERMISSION_READ_WEITE_CAMERA, this);
                    }
                    break;
                case R.id.tv_register_step_2_identity_statement://居民身份声明
                case R.id.iv_register_step_2_identity_statement://居民身份声明
                    currentType = 5;
                    identity_statementPopShow = true;
                    currentPosition = 0;
                    initPopList();
                    if (list != null && !list.isEmpty() && list.size() > 0) {
                        showPicker();
                    } else {
                        getTaxType(false);
                    }
                    break;
                case R.id.rl_tv_tax_live_address_country_layout://税收现居住地国别
                    currentType = 6;
                    currentPosition = 0;
                    initPopList();
                    if (list != null && !list.isEmpty() && list.size() > 0) {
                        showPicker();
                    } else {
                        getCountry(true);
                    }
                    break;
                case R.id.rl_tax_live_address_province_layout: //税收现居住地省

                    if (TextUtils.isEmpty(liveAddressCountryId)) {
                        showToast("请先选择国别");
                        return;
                    }

                    if (!isSelectTaxCountry(liveAddressCountryId)) {
                        return;
                    }
                    currentType = 11;
                    currentPosition = 0;
                    initPopList();

                    if (list != null && !list.isEmpty() && list.size() > 0) {
                        showPicker();
                    } else {
                        getTaxProvince(true);
                    }
                    break;
                case R.id.rl_tax_live_address_city_layout://税收现居住地市
                    if (TextUtils.isEmpty(liveAddressProvinceCode)) {
                        showToast("请先选择您现居地所在的省");
                        return;
                    }
                    currentType = 12;
                    currentPosition = 0;
                    taxCityList.clear();
                    initPopList();
                    if (list != null && !list.isEmpty() && list.size() > 0) {
                        showPicker();
                    } else {
                        getTaxCity(liveAddressProvinceCode, true);
                    }
                    break;
                case R.id.rl_tax_birthplace_address_country://税收出生地国别
                    currentType = 7;
                    currentPosition = 0;
                    initPopList();
                    if (list != null && !list.isEmpty() && list.size() > 0) {
                        showPicker();
                    } else {
                        getCountry(true);
                    }
                    break;
                case R.id.rl_tax_birthplace_address_province_layout://出生地省

                    if (TextUtils.isEmpty(birthAddressCountryId)) {
                        showToast("请先选择国别");
                        return;
                    }

                    if (!isSelectTaxCountry(birthAddressCountryId)) {
                        return;
                    }
                    currentType = 13;
                    currentPosition = 0;
                    initPopList();
                    if (list != null && !list.isEmpty() && list.size() > 0) {
                        showPicker();
                    } else {
                        getTaxProvince(true);
                    }
                    break;
                case R.id.rl_tax_birthplace_address_city_layout: //出生地市

                    if (TextUtils.isEmpty(birthAddressProvinceCode)) {
                        showToast("请先选择您出生地所在的省");
                        return;
                    }
                    currentType = 14;
                    currentPosition = 0;
                    taxCityList.clear();
                    initPopList();

                    if (list != null && !list.isEmpty() && list.size() > 0) {
                        showPicker();
                    } else {
                        getTaxCity(birthAddressProvinceCode, true);
                    }
                    break;
            }
        }
    }

    private void showDatePickerView() {
        //时间选择器
        datePickerView = new DatePickerView(mActivity, MentorConst.DateType.YEAR_MONTH_DAY);

        //控制时间范围
        Date date = new Date();//取时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.DATE, 1);//1往后一天，-1提前一天
//                Calendar calendar = Calendar.getInstance();
        //要在setTime 之前才有效果哦
        //System.out.println(calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + (calendar.get(Calendar.DAY_OF_MONTH)));
        datePickerView.setCurrentTime(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1,
                calendar.get(Calendar.DAY_OF_MONTH));
        datePickerView.setRange(calendar.get(Calendar.YEAR), 2118);
        datePickerView.setTime(new Date());
        datePickerView.setCyclic(false);
        datePickerView.setCancelable(true);
        //时间选择后回调
        datePickerView.setOnTimeSelectListener(mDatePickerSelectLinstener);
        datePickerView.show();
    }

    private String sex;
    private String profession;
    private String provinceCode;
    private String cityCode;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (datePickerView != null && datePickerView.isShowing()) {
                datePickerView.dismiss();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGestureSetCallBack(RegisterSuccessEvent event) {
        if (event.isSuccess) {
            finish();
        }
    }


    private String recommenderStr;

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public void onPermissionSuccess() {
        Intent it = new Intent(RegisterStep2Activity.this, CaptureActivity.class);
        startActivityForResult(it, CAPTURE);
    }

    @Override
    public void onPermissionFail() {
        ToastUtils.showShort("获取权限失败，请点击重试");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {

            case CAPTURE:
                if (resultCode == RESULT_OK) {
                    String result = data.getStringExtra("result");

                    if (TextUtils.isEmpty(result) || !result.contains(":") || !result.contains("ICCellPhone:")) {
                        Toast.makeText(this, "不支持此二维码解析", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    if (!TextUtils.isEmpty(result)) {
                        String[] arrys = null;
                        arrys = result.split("\n");

                        HashMap<String, String> valueMap = new HashMap<String, String>();
                        for (int i = 0; i < arrys.length; i++) {
                            String[] keyAndValue = arrys[i].split(":");
                            valueMap.put(keyAndValue[0], keyAndValue[1]);
                        }

                        Iterator<Map.Entry<String, String>> iterator = valueMap.entrySet().iterator();
                        String decryptingCode = "";
                        while (iterator.hasNext()) {
                            Map.Entry<String, String> entry = iterator.next();

                            if (entry.getKey().equals("ICCellPhone")) {
                                decryptingCode = AESUtils.decrypt("ICBCCS_Magus", entry.getValue());
                                try {
                                    recommenderStr = new String(decryptingCode.getBytes("ISO-8859-1"), "gbk");
                                    et_register_step_2_id_tjr.setText(recommenderStr);
                                } catch (UnsupportedEncodingException e) {
                                    e.printStackTrace();
                                } // 防止中文乱码
                            }
                        }
                        if (TextUtils.isEmpty(decryptingCode)) {
                            Toast.makeText(this, "不支持此二维码解析", Toast.LENGTH_SHORT).show();
                        }
                    }

                }
                break;
        }
    }

    //个人涉税逻辑 1：仅中国税收居民 2：仅非居民 3：即使中国税收居民又是他国税收居民
    private String identStatementOnlyChina = "ON";
    /**
     * 居民身份申明
     */
    private String identStatement = "";//居民身份申明
    private String liveAddressCountryId;//现居地国别
    private String liveAddressProvinceCode = "";//现居地 - 省
    private String liveAddressCityCode = "";//现居地 - 市
    private String birthAddressCountryId;//出生地国别
    private String birthAddressProvinceCode = "";//出生地 - 省
    private String birthAddressCityCode = "";//出生地 - 市

    private boolean identity_statementPopShow = false;//居民身份申明 选择框是否弹出


    /**
     * 个人涉税布局初始化
     */
    private void taxLayoutInit() {
        if (registerEvent == null || "8".equals(registerEvent.getIdentityType()) || "9".equals(registerEvent.getIdentityType()) || "2".equals(registerEvent.getIdentityType())) {
            ll_tax_layout.setVisibility(View.GONE);
            return;
        }
        getTaxType(true);
        ll_tax_layout.setVisibility(View.VISIBLE);
        taxLayoutShow(identStatement);

    }

    /**
     * 根据选择的 居民身份 进行判断初始化那些 有关涉税的布局模块
     *
     * @param identStatement 1：仅中国税收居民 2：仅非居民 3：即使中国税收居民又是他国税收居民
     */
    private void taxLayoutShow(String identStatement) {
        ll_tax_user_info_layout.setVisibility(View.GONE);//个人信息
        ll_tax_live_address_layout.setVisibility(View.GONE);//现居住地址
        ll_tax_birthplace_address_layout.setVisibility(View.GONE);//出生地
        tv_other_taxpayer.setVisibility(View.GONE);//添加其他纳税人
        //3.0.8新增 提示入口
        rl_tips_flag.setVisibility(View.INVISIBLE);
        if (StringUtils.isNullOrEmpty(identStatement)) {
            return;
        }
        if (identStatementOnlyChina.equals(identStatement)) {
            tv_other_taxpayer.setVisibility(View.GONE);//添加其他纳税人
            //3.0.8新增
            rl_tips_flag.setVisibility(View.VISIBLE);
            taxPayersTotal.clear();
            taxPayersTotal.addAll(taxPayersContainer);
            clearRecyclerView();
        } else {
            if ("OF".equals(identStatement)) {
                rl_tips_flag.setVisibility(View.VISIBLE);
            }
            ll_tax_user_info_layout.setVisibility(View.VISIBLE);//个人信息
            ll_tax_live_address_layout.setVisibility(View.VISIBLE);//现居住地址
            ll_tax_birthplace_address_layout.setVisibility(View.VISIBLE);//出生地
            tv_other_taxpayer.setVisibility(View.VISIBLE);//税收居民国及纳税人识别号
            taxPayersContainer.clear();
            taxPayersContainer.addAll(taxPayersTotal);
        }
    }

    /**
     * 对个人涉税相关信息进行检查  填写是否合理
     *
     * @return
     */
    private boolean paramsCheckTax() {
        if (registerEvent == null) {
            return false;
        }
        if ("8".equals(registerEvent.getIdentityType()) || "9".equals(registerEvent.getIdentityType()) || "2".equals(registerEvent.getIdentityType())) {
            return true;
        }
        if (checkParam(identStatement, "请选择居民身份")) {
            return false;
        }
        if (identStatementOnlyChina.equals(identStatement)) {
            if (!cb_ident_shengming_info.isChecked()) {
                showConfirmAlert(mActivity, "提示", "请勾选声明确认");
                return false;
            }
            return true;
        }

        if (checkParam(et_tax_surname.getText().toString(), "请输入姓")) {
            et_tax_surname.setIsSelected(true);
            return false;
        }

        if (checkParam(et_tax_name.getText().toString(), "请输入名")) {
            et_tax_name.setIsSelected(true);
            return false;
        }

        if (checkParam(liveAddressCountryId, "请选择现居地国别")) {
            return false;
        }
        if (isSelectTaxCountry(liveAddressCountryId)) {
            if (checkParam(liveAddressProvinceCode, "请选择现居地省")) {
                return false;
            }
            if (checkParam(liveAddressCityCode, "请选择现居地市")) {
                return false;
            }
            if (checkParam(et_tax_live_chinese_address.getText().toString(), "请输入现居地中文地址")) {
                et_tax_live_chinese_address.setIsSelected(true);
                return false;
            }
            if (checkParam(et_tax_live_pinyin_address.getText().toString(), "请输入现居地拼音地址")) {
                et_tax_live_pinyin_address.setIsSelected(true);
                return false;
            }
        } else {
            if (checkParam(et_tax_live_english_address.getText().toString(), "请输入现居地英文地址")) {
                et_tax_live_english_address.setIsSelected(true);
                return false;
            }
        }

        if (checkParam(birthAddressCountryId, "请选择出生地国别")) {
            return false;
        }

        if (isSelectTaxCountry(birthAddressCountryId)) {
            if (checkParam(birthAddressProvinceCode, "请选择出生地省")) {
                return false;
            }
            if (checkParam(birthAddressCityCode, "请选择出生地市")) {
                return false;
            }
        } else {
            if (checkParam(hwet_tax_birthplace_address_province.getText().toString(), "请输入出生地省")) {
                hwet_tax_birthplace_address_province.setIsSelected(true);
                return false;
            }

            if (checkParam(hwet_tax_birthplace_address_city.getText().toString(), "请输入出生地省")) {
                hwet_tax_birthplace_address_city.setIsSelected(true);
                return false;
            }
        }

        if (!ParamUtil.checkedTaxPayersParam(taxPayersTotal)) {
            showConfirmAlert(this, "请补全其他纳税人信息");
            return false;
        }
        if (!cb_ident_shengming_info.isChecked()) {
            showConfirmAlert(mActivity, "提示", "请勾选声明确认");
            return false;
        }

        return true;
    }

    /**
     * 绑定涉税数据
     */
    private void bindTaxData() {

        if (registerEvent == null || "8".equals(registerEvent.getIdentityType()) || "9".equals(registerEvent.getIdentityType()) || "2".equals(registerEvent.getIdentityType())) {
            return;
        }
        registerEvent.setTaxType(identStatement);//税收类型

        if (identStatementOnlyChina.equals(identStatement)) {
            return;
        }

        registerEvent.setNameEn(et_tax_name.getText().toString());//名
        registerEvent.setSurnameEn(et_tax_surname.getText().toString());//姓
        registerEvent.setDomicileCountry(liveAddressCountryId);//居住地-国家

        if (isSelectTaxCountry(liveAddressCountryId)) {
            registerEvent.setDomicileProvince(liveAddressProvinceCode);//居住地-省份
            registerEvent.setDomicileCity(liveAddressCityCode);//居住地-城市
            registerEvent.setDomicileAddressCn(et_tax_live_chinese_address.getText().toString());//居住地-地址-中文
            registerEvent.setDomicileAddressEn(et_tax_live_pinyin_address.getText().toString());//居住地-地址-拼音
        } else {
            registerEvent.setDomicileProvince(StringUtils.checkParam(hwet_tax_live_address_province.getText().toString()));//居住地
            registerEvent.setDomicileCity(StringUtils.checkParam(hwet_tax_live_address_city.getText().toString()));//居住地
            registerEvent.setDomicileAddressEn(StringUtils.checkParam(et_tax_live_english_address.getText().toString()));//居住地-地址-英文
        }

        registerEvent.setBirthplaceCountry(birthAddressCountryId);//出生地-国家

        if (isSelectTaxCountry(birthAddressCountryId)) {
            registerEvent.setBirthplaceProvince(birthAddressProvinceCode);//出生地-省份
            registerEvent.setBirthplaceCity(birthAddressCityCode);//出生地-城市
        } else {
            registerEvent.setBirthplaceProvince(StringUtils.checkParam(hwet_tax_birthplace_address_province.getText
                    ().toString()));//出生地-省份
            registerEvent.setBirthplaceCity(StringUtils.checkParam(hwet_tax_birthplace_address_city.getText()
                    .toString()));//出生地-城市
        }

        //纳税人集合数据
        registerEvent.setTaxPayers(ParamUtil.formatTaxPayList(taxPayersTotal));
    }

    /**
     * 获取涉税类型
     *
     * @param isOnCreate
     */
    private void getTaxType(final boolean isOnCreate) {
        Map<String, String> params = new HashMap<>();
        params.put("clientType", HostConstant.CLIENT_TYPE);//客户端类型
        OkGo.<String>post(HostConstant.QUERY_TAXTYPE)
                .tag(this)
                .upJson(new Gson().toJson(params))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        MentorLogger.LogJson(TAG + "涉税类型:", response.body());
                        parseTaxTypeJson(response.body(), isOnCreate);
                    }


                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }
                });
    }

    private void getCountry(final boolean isShowPop) {
        Map<String, String> params = new HashMap<>();
        params.put("clientType", HostConstant.CLIENT_TYPE);//客户端类型
        OkGo.<String>post(HostConstant.QUERY_TAX_COUNTRY)
                .tag(this)
                .upJson(new Gson().toJson(params))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseCountryJson(response.body(), isShowPop);
                    }


                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }
                });
    }

    private List<QueryCountryBean.DataBean> countryList = new ArrayList<>();
    private List<QueryCountryBean.DataBean> taxTypeList = new ArrayList<>();

    private void parseTaxTypeJson(String json, boolean isOnCreate) {
        taxTypeList.clear();
        list.clear();
//
        QueryCountryBean bean;
        try {
            bean = new Gson().fromJson(json, QueryCountryBean.class);
        } catch (Exception exception) {
            showConfirmAlert(mActivity, "提示", "数据解析错误");
            return;
        }

        if (TextUtils.isEmpty(json) && !CommonUtil.isNetWorkConnected(mActivity)) {
            showToast(MentorConst.NO_NETWORK_TIP);
            return;
        }

        if (bean == null
                || TextUtils.isEmpty(bean.getErrorCode())) {
            showToast(MentorConst.SERVER_BUSY_TIP);
            return;
        }

        BaseBean baseBean = new BaseBean();
        baseBean.setRespCode(bean.getErrorCode());
        baseBean.setRespMsg(bean.getErrorMessage());
        if (!CommonUtil.checkState(mActivity, baseBean, json)) {
            return;
        }

        if (bean == null || bean.getData() == null ||
                bean.getData().isEmpty() || bean.getData().size() < 1) {
            showConfirmAlert(mActivity, "提示", getString(R.string.no_data));
            return;
        }

        taxTypeList.addAll(bean.getData());

        for (int i = 0; i < taxTypeList.size(); i++) {
            if ("NO".equals(taxTypeList.get(i).getItemValue())) {
                taxTypeList.remove(i);
                break;
            }
        }
        if (isOnCreate) {
            for (int i = 0; i < taxTypeList.size(); i++) {
                if ("ON".equals(taxTypeList.get(i).getItemValue())) {
                    tv_register_step_2_identity_statement.setText(taxTypeList.get(i).getItemName());
                    identStatement = taxTypeList.get(i).getItemValue();
                    taxLayoutShow(identStatement);
                    break;
                }
            }
        }
        initPopList();

        if (identity_statementPopShow) {
            showPicker();
        }
    }

    private void parseCountryJson(String json, boolean isShowPop) {
        countryList.clear();
        list.clear();
//
        QueryCountryBean bean;
        try {
            bean = new Gson().fromJson(json, QueryCountryBean.class);
        } catch (Exception exception) {
            showConfirmAlert(mActivity, "提示", "数据解析错误");
            return;
        }

        if (TextUtils.isEmpty(json) && !CommonUtil.isNetWorkConnected(mActivity)) {
            showToast(MentorConst.NO_NETWORK_TIP);
            return;
        }

        if (bean == null
                || TextUtils.isEmpty(bean.getErrorCode())) {
            showToast(MentorConst.SERVER_BUSY_TIP);
            return;
        }

        BaseBean baseBean = new BaseBean();
        baseBean.setRespCode(bean.getErrorCode());
        baseBean.setRespMsg(bean.getErrorMessage());
        if (!CommonUtil.checkState(mActivity, baseBean, json)) {
            return;
        }

        if (bean == null || bean.getData() == null ||
                bean.getData().isEmpty() || bean.getData().size() < 1) {
            showConfirmAlert(mActivity, "提示", getString(R.string.no_data));
            return;
        }

        countryList.addAll(bean.getData());
        initPopList();

        if (isShowPop) {
            showPicker();
        }
    }

    private String getSelectProvinceCode(String countryId, int selectPosition) {
        if (ParamUtil.isSelectTaxCountry(countryId)) {
            return taxProvinceList.get(selectPosition).getRegionCode();
        }
        return "";
    }

    private class FooterViewClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_register_step_2:
                    nextStepClick();
                    break;
                case R.id.tv_other_taxpayer: //其他纳税人,增加条目
                    if (taxPayersContainer != null && Math.min(taxPayersContainer.size(), maxTaxpayerSize) < maxTaxpayerSize) {
                        taxPayerAdapter.addData(taxPayerAdapter.getData().size());
                        taxPayersTotal.clear();
                        taxPayersTotal.addAll(taxPayerAdapter.getData());
                    } else {
                        showToast("超出可添加纳税人数量");
                    }
                    break;
            }
        }
    }

    /**
     * 点击下一步校验
     */
    private void nextStepClick() {
        AnalyticsEventUtils.toFillInfoEvent(this);
        rootview.setFocusable(true);
        rootview.setFocusableInTouchMode(true);
        registEmil = et_register_step_2_emil.getText().toString().trim();
        registPostCode = et_register_step_2_postcode.getText().toString().trim();
        registAddress = et_register_step_2_address.getText().toString().trim();
        registOccupation = et_register_step_2_duty.getText().toString().trim();
        controllerName = et_others_control_name.getText().toString().trim();
        controllerCertNo = et_control_identity.getText().toString().trim();
        controllerCertValidTerm = tv_control_identity_validity.getText().toString().trim();
        controllerInstruction = et_control_cause.getText().toString().trim();

        beneficiaryName = et_beneficial_owner_name.getText().toString().trim();
        beneficiaryCertNo = et_beneficial_owner_identity.getText().toString().trim();
        beneficiaryCertValidTerm = tv_beneficial_identity_validity.getText().toString().trim();
        beneficiaryInstruction = et_beneficial_owner_cause.getText().toString().trim();

        badCreditRecords = et_bad_record.getText().toString().trim();

        if (!cb_register_step_2_id_time.isChecked() && StringUtils.isNullOrEmpty(tv_register_step_2_id_time.getText().toString())) {
            showConfirmAlert(this, "温馨提示", "身份证有效期获取失败，请联系客服:400-811-9999解决。", "放弃", "呼叫客服");
            return;
        }

        if (checkParam(profession, "请选择您的职业")) {
            return;
        }

        if (checkParam(registOccupation, "请输入您的职务")) {
            et_register_step_2_duty.setIsSelected(true);
            return;
        }

        if (checkParam(tv_register_step_2_birthday.getText().toString().replace("-", ""), "请选择您的出生日期")) {
            return;
        }
        if (checkParam(registEmil, "请输入邮箱")) {
            et_register_step_2_emil.setIsSelected(true);
            return;
        } else {
            if (!StringUtils.isEmail(et_register_step_2_emil.getText().toString().trim())) {
                et_register_step_2_emil.setIsSelected(true);
                showConfirmAlert(RegisterStep2Activity.this, "邮箱格式不正确");
                return;
            }
        }

        if (checkParam(provinceCode, "请选择省份")) {
            return;
        }
        if (checkParam(cityCode, "请选择城市")) {
            return;
        }

        if (checkParam(registPostCode, "请输入邮编")) {
            et_register_step_2_postcode.setIsSelected(true);
            return;
        } else {
            if ((registPostCode.length() != 6)) {
                et_register_step_2_postcode.setIsSelected(true);
                showConfirmAlert(mActivity, "邮编不合法");
                return;
            }
        }

        if (checkParam(registAddress, "请输入详细地址")) {
            et_register_step_2_address.setIsSelected(true);
            return;
        }

        //3.0.7增加 账户他人控制
        if (sbtn_is_others_control.isChecked()) {
            if (checkParam(controllerName, "请输入控制人姓名")) {
                et_others_control_name.setIsSelected(true);
                return;
            }
            if (checkParam(controllerCertType, "请选择控制人证件类型")) {
                return;
            }
            if (checkParam(controllerCertNo, "请输入控制人证件号")) {
                et_control_identity.setIsSelected(true);
                return;
            } else {
                if ("0".equals(controllerCertType)) {
                    if (!StringUtils.isIdentity(controllerCertNo)) {
                        et_control_identity.setIsSelected(true);
                        showConfirmAlert(this, "请输入合规身份证号");
                        return;
                    }
                }
            }
            if (!cb_control_identity_validity_time.isChecked() && checkParam(controllerCertValidTerm,
                    "请选择控制人证件有效期")) {
                return;
            }
            if (checkParam(controllerInstruction, "请说明有此控制关系的原因")) {
                et_control_cause.setIsSelected(true);
                return;
            }
        }

        //3.0.7增加 受益人

        if (!sbtn_is_beneficial_owner.isChecked()) {
            if (checkParam(beneficiaryName, "请输入受益人姓名")) {
                et_beneficial_owner_name.setIsSelected(true);
                return;
            }
            if (checkParam(beneficiaryCertType, "请选择受益人证件类型")) {
                return;
            }
            if (checkParam(beneficiaryCertNo, "请输入受益人证件号")) {
                et_beneficial_owner_identity.setIsSelected(true);
                return;
            } else {
                if ("0".equals(beneficiaryCertType)) {
                    if (!StringUtils.isIdentity(beneficiaryCertNo)) {
                        et_beneficial_owner_identity.setIsSelected(true);
                        showConfirmAlert(this, "请输入合规身份证号");
                        return;
                    }
                }
            }
            if (!cb_beneficial_identity_validity_time.isChecked() && checkParam(beneficiaryCertValidTerm,
                    "请选择受益人证件有效期")) {
                return;
            }
            if (checkParam(beneficiaryInstruction, "请说明受益人不是本人的原因")) {
                et_beneficial_owner_cause.setIsSelected(true);
                return;
            }
        }

        //3.0.7新增不良记录
        if (sbtn_is_bad_record.isChecked()) {
            if (checkParam(badCreditRecords, "请输入不良记录内容")) {
                et_bad_record.setIsSelected(true);
                return;
            }
        }
        if (paramsCheckTax()) {
            bindTaxData();
        } else {
            return;
        }
        registerEvent.setProvinceCode(provinceCode);
        registerEvent.setCityCode(cityCode);
        registerEvent.setIdValidDate(tv_register_step_2_id_time.getText().toString());
        registerEvent.setProfession(profession);
        registerEvent.setSex(sex);
        registerEvent.setCertificateValidEver(cb_register_step_2_id_time.isChecked() ? "1" : "0");
        registerEvent.setBirthday(tv_register_step_2_birthday.getText().toString().replace("-", ""));
        registerEvent.setRecommendPerson(et_register_step_2_id_tjr.getText().toString().trim());

        //3.0.5新增
        registerEvent.setAddress(registAddress);
        registerEvent.setEmail(registEmil);
        registerEvent.setPostCode(registPostCode);

        //3.0.7新增账户他人控制
        registerEvent.setOccupation(registOccupation); //职务
        registerEvent.setIsExistActualControl(sbtn_is_others_control.isChecked() ? "1" : "0");
        registerEvent.setControllerName(controllerName);
        registerEvent.setControllerCertType(controllerCertType);
        registerEvent.setControllerCertNo(controllerCertNo);
        registerEvent.setControllerCertValidTerm(controllerCertValidTerm.replace("-", ""));
        registerEvent.setControllerCertValidEver(cb_control_identity_validity_time.isChecked() ? "1" : "0");
        registerEvent.setControllerInstruction(controllerInstruction);
        //3.0.7新增 受益人
        registerEvent.setActualBeneficiaryRelation(sbtn_is_beneficial_owner.isChecked() ? "0" : "1");
        registerEvent.setBeneficiaryName(beneficiaryName);
        registerEvent.setBeneficiaryCertType(beneficiaryCertType);
        registerEvent.setBeneficiaryCertNo(beneficiaryCertNo);
        registerEvent.setBeneficiaryCertValidTerm(beneficiaryCertValidTerm.replace("-", ""));
        registerEvent.setBeneficiaryCertValidEver(cb_beneficial_identity_validity_time.isChecked() ? "1" : "0");
        registerEvent.setBeneficiaryInstruction(beneficiaryInstruction);

        //3.0.7 新增不良记录
        registerEvent.setHasBadCreditRecords(sbtn_is_bad_record.isChecked() ? "1" : "0");
        registerEvent.setBadCreditRecords(badCreditRecords);

        Intent intent = new Intent().setClass(mActivity, RegisterStep3Activity.class);
        intent.putExtra("registerEvent", registerEvent);
        MentorLogger.Loge(registerEvent.toString());
        startActivity(intent);
    }


    class InputTextWatcher implements TextWatcher {

        private HWEditText inputContent;
        private String desc;

        public InputTextWatcher(HWEditText inputContent, String desc) {
            this.inputContent = inputContent;
            this.desc = desc;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
            String str = this.inputContent.getText().toString().trim();
            if (str.length() > 60) {
                this.inputContent.setIsSelected(true);
                showConfirmAlert(RegisterStep2Activity.this, desc);
            } else {
                this.inputContent.setIsSelected(false);
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    }
}
